<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item
        label="组名"
        prop="name"
      >
        <el-input
          v-model="dataForm.name"
          placeholder="组名"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="排序"
        prop="sort"
      >
        <el-input
          v-model="dataForm.sort"
          placeholder="排序"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="描述"
        prop="description"
      >
        <el-input
          v-model="dataForm.description"
          placeholder="描述"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="组图标"
        prop="icon"
      >
        <el-input
          v-model="dataForm.icon"
          placeholder="组图标"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="所属分类"
        prop="categoryId"
      >
        <category-cascader :categoryPath.sync="categoryPath"></category-cascader>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategoryCascader from '../common/category-cascader'
export default {
  data () {
    return {
      props: {
        value: 'catId',
        label: 'name',
        children: 'childCategory'
      },
      visible: false,
      categorys: [],
      categoryPath: [],
      dataForm: {
        id: 0,
        name: '',
        sort: '',
        description: '',
        icon: '',
        categoryId: 0,
        categoryPath: []
      },
      dataRule: {
        name: [
          { required: true, message: '组名不能为空', trigger: 'blur' }
        ],
        sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ],
        icon: [{ required: true, message: '组图标不能为空', trigger: 'blur' }],
        categoryId: [
          { required: true, message: '所属分类id不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  components: {CategoryCascader},

  methods: {
    dialogClose () {
      this.categoryPath = []
    },
    getCategorys () {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then(({ data }) => {
        this.categorys = data.data
        console.log(this.categorys)
        console.log('1')
      })
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/attrGroup/info/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              console.log('~~~***')
              this.dataForm.name = data.attrGroup.name
              this.dataForm.sort = data.attrGroup.sort
              this.dataForm.description = data.attrGroup.description
              this.dataForm.icon = data.attrGroup.icon
              this.dataForm.categoryId = data.attrGroup.categoryId
              // 查出categoryId的完整路径
              this.dataForm.categoryPath = data.attrGroup.categoryPath
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          console.log('qwe')
          console.log(this.categoryPath)
          this.$http({
            url: this.$http.adornUrl(
              `/product/attrGroup/${
                !this.dataForm.id ? 'save' : 'update'
              }`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              sort: this.dataForm.sort,
              description: this.dataForm.description,
              icon: this.dataForm.icon,
              categoryId: this.categoryPath[this.categoryPath.length - 1]
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  },
  created () {
    this.getCategorys()
  }
}
</script>
